package com.tbit.uqbike.object.view.sugar;

import com.tbit.utils.DateUtil;
import org.springframework.web.servlet.support.RequestContext;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author MyWin E-mail:335918956@qq.com
 * @version 1.0
 * @createTime 2020-02-06 9:42
 */
public class HistogramData {
    public List<String> categories = new LinkedList<>();
    public List<SeriesItem> series = new LinkedList<>();

    public static HistogramData buildDayModel(int days) {
        HistogramData data = new HistogramData();
        for (int i = 0; i < days; i++) {
            String str = DateUtil.YMD_.format(System.currentTimeMillis() - DateUtil.DAY_MS * i);
            data.categories.add(str);
        }
        return data;
    }

    public void initData(Map<String, Map<Integer, Integer>> map, RequestContext context) {
        List<Integer> alarmCodeList = new LinkedList() {
            {
                add(3);
                add(5);
                add(8);
            }
        };
        for (Integer code : alarmCodeList) {
            SeriesItem si = new SeriesItem();
            si.name = String.format("alarm.%d", code);
            si.name = context.getMessage(si.name);
            for (String dt : categories) {
                if (map.containsKey(dt) && map.get(dt).containsKey(code)) {
                    si.data.add(Double.valueOf(map.get(dt).get(code)));
                } else {
                    si.data.add(0.0);
                }
            }
            series.add(si);
        }
    }

    public static class SeriesItem {
        public String name;
        public List<Double> data = new LinkedList<>();
    }
}
